﻿using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using Unity.VisualScripting.FullSerializer;
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.SubsystemsImplementation;

public class GameManager : MonoBehaviour
{

    //单例模式
    private static GameManager instance;
    public static GameManager Instance
    {
        get
        {
            if (instance == null)
            {
                //GameObject temp= new GameObject(nameof(GameManager));
                //instance=temp.AddComponent<GameManager>();
                instance=GameObject.FindWithTag("GameManagerTag").GetComponent<GameManager>();

            }
            return instance;
        }
    }

    public enum ItemType
    {
        Empty,
        Normal,
        Special
    }
    [Serializable]
    public struct Cell
    {
        public ItemType itemType;
        public GameObject cell;
    }
    public Cell[] itemPrefabs;
    public GameObject backGround;
    public int xNum;
    public int yNum;
    
     
    private Dictionary<ItemType, GameObject> itemPrefabDict;

    private GameBat startBat;
    private GameBat endBat;

    private GameBat[,] bats;
    private List<GameBat> sameBatList;
    
    // Start is called before the first frame update
    void Start()
    {
        bats = new GameBat[xNum, yNum];
        sameBatList = new List<GameBat>();
        
       
        //实例化字典，并将不同状态添加进字典
        itemPrefabDict = new Dictionary<ItemType, GameObject>();
        for(int i=0;i<itemPrefabs.Length; i++)
        {
            if (!itemPrefabDict.ContainsKey(itemPrefabs[i].itemType))
            {
                itemPrefabDict.Add(itemPrefabs[i].itemType, itemPrefabs[i].cell);
            }
        }
        CreatGrid(xNum, yNum);
  
        CreatBat(xNum, yNum);

        Bianliqiupai();
        
        
    }
    public void Bianliqiupai()
    {
        for (int i = 0; i < xNum; i++)
        {
            for (int j = 0; j < yNum; j++)
            {
                Debug.Log(bats[i, j].ColorBatComponent.ReadColorName());
            }
        }
        Debug.Log("+++++++++++++++++++++++++++++++++++++++");
    }



    // Update is called once per frame
    void Update()
    {
        
    }

    //生成网格
    public void CreatGrid(int m,int n)
    {
        for(int i=0;i<m;i++)
        {
            for(int j = 0; j < n; j++)
            {
                GameObject temp = Instantiate(itemPrefabDict[ItemType.Empty],new Vector3(5,5,0),Quaternion.identity,backGround.transform);
                temp.transform.localScale = new Vector3((float)0.1, (float)0.1, 0);
                temp.transform.localPosition=CorrectPosition(i,j);

            }
        }
    }
    //第一次生成球拍
    public void CreatBat(int m,int n)
    {
        for(int i=0;i<m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                GameObject temp = Instantiate(itemPrefabDict[ItemType.Normal],new Vector3(5,5,0), Quaternion.identity,backGround.transform);
                temp.transform.localScale=new Vector3(7.8f, 7.8f, 0);
                temp.transform.localPosition =CorrectPosition(i,j);
                bats[i,j]=temp.GetComponent<GameBat>();
                bats[i, j].Deliver(i,j,ItemType.Normal);
                bats[i, j].ColorBatComponent.SetBatColor((ColorBat.ColorType)UnityEngine.Random.Range (0, bats[i, j].ColorBatComponent.ColorBats.Length));
            }
        }
    }
    //纠正位置
    public Vector3 CorrectPosition(int m,int n) 
    { 
        Vector3 startPosition= new Vector3();
        startPosition.x = -2.57f;
        startPosition.y = 4.87f;
        float aDistance = 1.09f;
        float bDistance = 0.74f;
        Vector3 resualt = new Vector3(startPosition.x+m*bDistance,startPosition.y-n*aDistance,0);
        return resualt;
        
    }
    

    //查找相同的元素，使用递归算法
    public void FindSame(GameBat tempBat)
    {
        int x=tempBat.X; 
        int y=tempBat.Y;
        sameBatList.Add(tempBat);

        if ((x - 1 >= 0 && x + 1 <= xNum-1)|| (y - 1 >= 0 && y + 1 <= yNum - 1))
        {
            //左边
            if (bats[x - 1, y].ColorBatComponent.ReadColorName() == tempBat.ColorBatComponent.ReadColorName())
            {
                if (sameBatList.Contains(bats[x - 1, y]))//这里的判断是为了避免重复添加
                {

                }
                else
                {
                    FindSame(bats[x - 1, y]);
                }

            }
            //右边
            else if (bats[x + 1, y].ColorBatComponent.ReadColorName() == tempBat.ColorBatComponent.ReadColorName())
            {
                if (sameBatList.Contains(bats[x + 1, y]))
                {

                }
                else
                {
                    FindSame(bats[x + 1, y]);
                }

        }
            //上边
            else if (bats[x, y - 1].ColorBatComponent.ReadColorName() == tempBat.ColorBatComponent.ReadColorName())
            {
                if (sameBatList.Contains(bats[x, y - 1]))
                {

                }
                else
                {
                    FindSame(bats[x, y - 1]);
                }
                Debug.Log("向上检查");
            }
            //下边
            else if (bats[x, y + 1].ColorBatComponent.ReadColorName() == tempBat.ColorBatComponent.ReadColorName())
            {
                if (sameBatList.Contains(bats[x, y + 1]))
                {

                }
                else
                {
                    FindSame(bats[x, y + 1]);
                }
                //Debug.Log("向下检查");
            }
        }
        
        
    }
    ////行查找
    //public void LineSearching(GameBat currentBat)
    //{
    //    int x1 = currentBat.X;
    //    int y1=currentBat.Y;
        
    //    sameLineBatList.Add(currentBat);
    //    for(int i=x1;i<xNum;i++)
    //    {
    //        if (bats[i,y1].ColorBatComponent.ReadColorName()==currentBat.ColorBatComponent.ReadColorName())
    //        {
    //            if (!sameLineBatList.Contains(bats[i,y1]))
    //            {
                    
    //                sameLineBatList.Add(bats[i, y1]);
    //            }
                
    //        }
    //        else
    //        {
    //            break;
    //        }
    //    }
    //    for(int i = x1; i >= 0; i--)
    //    {
    //        if (bats[i, y1].ColorBatComponent.ReadColorName() == currentBat.ColorBatComponent.ReadColorName())
    //        {
    //            if (!sameLineBatList.Contains(bats[i, y1]))
    //            {

    //                sameLineBatList.Add(bats[i, y1]);
    //            }
    //        }
    //        else
    //        {
    //            break;
    //        }
    //    }

    //}
    ////列查找
    //public void ColumnSearch(GameBat currentBat)
    //{
    //    int x3=currentBat.X;
    //    int y3=currentBat.Y;
    //    //列当中不进行起始物体，避免了一次重复
    //    for(int i = y3; i < yNum; i++)
    //    {
    //        if (bats[x3, i].ColorBatComponent.ReadColorName() == currentBat.ColorBatComponent.ReadColorName())
    //        {
    //            if (!sameColumnBatList.Contains(bats[x3,i]))
    //            {
    //                sameColumnBatList.Add(bats[x3, i]);
    //            }
                
    //        }
    //        else 
    //        { 
    //            break; 
    //        }
    //    }
    //    for(int i=y3;y3>=0; i--)
    //    {
    //        if (bats[x3, i].ColorBatComponent.ReadColorName() == currentBat.ColorBatComponent.ReadColorName())
    //        {
    //            if (!sameColumnBatList.Contains(bats[x3, i]))
    //            {
    //                sameColumnBatList.Add(bats[x3, i]);
    //            }
    //        }
    //        else
    //        {
    //            break;
    //        }
    //    }
    //}
    //等待消除
    public void Eliminate(GameBat temBat)
    {
        List<GameBat> lineList= new List<GameBat>();
        List<GameBat> columnList= new List<GameBat>();
        foreach(GameBat bat in sameBatList)
        {
            if (bat.X == temBat.X)
            {
                lineList.Add(bat);
            }
            if(bat.Y == temBat.Y)
            {
                columnList.Add(bat);
            }
        }
        if(lineList.Count >=3)
        {
            Debug.Log("行的相同数量达到三个，可进行删除");
        }
        if(columnList.Count >= 3)
        {
            //移除基准，避免多次删除
            columnList.Remove(temBat);
            Debug.Log("列的相同数量达到三个，可进行删除");
        }



        
    }

    //判断两个物体是否相邻
    public bool IsNear(GameBat gameBat1,GameBat gameBat2)
    {
       
        if((gameBat1.X==gameBat2.X&& Mathf.Abs(gameBat1.Y-gameBat2.Y)==1)||(gameBat1.Y == gameBat2.Y && Mathf.Abs(gameBat1.X - gameBat2.X) == 1))
        {
            return true;
        }
        else
        {
            return false;
        }
        

    }
    //交换两个球拍位置
    public void ExchangeBats(GameBat gameBat1, GameBat gameBat2)
    {
        //GameObject temp1=new GameObject(nameof(GameBat));
        //GameBat firstBat=temp1.AddComponent<GameBat>();
        GameBat firstBat = gameBat1;
        GameBat secondBat = gameBat2;
        int x1=firstBat.X;
        int y1=firstBat.Y;
        int x2=secondBat.X;
        int y2=secondBat.Y;

        bats[firstBat.X, firstBat.Y]= gameBat2;
        bats[secondBat.X,secondBat.Y] = gameBat1;
        gameBat1.MoveBatComponent.Move(firstBat,secondBat);
        //gameBat1.Deliver(secondBat.X,secondBat.Y);
        //gameBat2.Deliver(firstBat.X,firstBat.Y);
        gameBat1.Deliver(x2, y2);
        gameBat2.Deliver(x1,y1);
        FindSame(gameBat1);
        foreach (GameBat b in sameBatList)
        {
            Debug.Log("值" + b.X + ", " + b.Y);
        }
        Debug.Log("++++++++++++++++++++++"+sameBatList.Count);
        Eliminate(gameBat1);


    }




    //鼠标的三种状态传递
    public void SecondBat(GameBat temp)
    {
        endBat = temp;
        Debug.Log("第2个的坐标" + endBat.Y);
            

    }
    public void FirstBat(GameBat temp)
    {
        startBat = temp;
        //可以播放选中动画
        Debug.Log("第一个的坐标" + startBat.Y);
        
    }
    public void ReleaseBat()
    {
        
        //如果两个物体相邻，即可进行交换，否则不行
        if (IsNear(startBat, endBat))
        {
            ExchangeBats(startBat, endBat);
            
        }

    }
}
